#statsNamespaceTenure.py
#Collects stats from files of namespace by startyear
#
#Health of Online Communities


#This is a new version of statsNamespaceTenure.py
#Currently, we use another method to calculate tenure and offset. We set the start of offset year 0 as the date when the first edit was made. And then we add one year to it to get #the starting point of offset year 1. 
#For instance, if the first edit was made on 2004-06-02, then this date is the start of offset year 0, and the start of offset year 1 is 2005-06-02.
#More examples: if date1 = "2008-08-18" and date2 = "2009-08-17", then the difference is 0 year.
#               if date1 = "2008-08-18" and date2 = "2009-09-17", then the difference is 1 year.  
#We take the last two tenure groups and merge them together since we do not have the entire database for 2012. So in this case, for example, for users who start in 2004, tenures 7 #and 8 get merged into tenure 7+.  
#Hence all users who start in 2011 get merged into group 201100 (start year:2011, offset:0, tenure:0). And for user who start in 2012, we also merge them into 2011.     

#Editor: Guanyu Wang


import os
import collections
import sys
import multiprocessing
import time

def main():
	
	inputDir = sys.argv[1]
	outputDir = sys.argv[2]

	inputFileList = os.listdir(inputDir)

	workers = []
	
	for inputFileName in inputFileList:
		print inputFileName
		
		inputFilePath = inputDir+'/'+inputFileName
		outputFilePath = outputDir+'/'+inputFileName
		
		if os.path.isfile(inputFilePath):

			worker = StatWorker(inputFilePath, outputFilePath)
			workers.append(worker)
			worker.start()

	for worker in workers:
		worker.join()

class StatWorker(multiprocessing.Process):

	def __init__(self, inputFilePath, outputFilePath):
		multiprocessing.Process.__init__(self)
		self.inputFilePath = inputFilePath
		self.outputFilePath = outputFilePath

	def run(self):
		stats(self.inputFilePath, self.outputFilePath)
		print "Finished statistics for " + self.inputFilePath + " in " + self.outputFilePath + "\n"
			
		
		
#prints out the facts
def stats(inputFilePath, outputFilePath):
	inputFileHandle = open(inputFilePath,"r")
	outputFileHandle = open(outputFilePath, 'a+')

	statDict = collections.defaultdict(int)
	outputFileHandle.write("Stats for " + inputFilePath)
	
        fmt = "%Y-%m-%d"       

	for line in inputFileHandle:
		pieces = line.split('\t')
		currenttime = pieces[1].split(' ')[0]  #eg.2010-08-02 04:19:04 --> 2010-08-02
                dc = time.strptime(currenttime,fmt)
		starttime = pieces[3].split(' ')[0]  #eg.2006-08-11 06:50:49 --> 2006-08-11
                ds = time.strptime(starttime,fmt)
		endtime = pieces[4].split(' ')[0]   #eg.2012-04-30 02:41:04 --> 2012-04-30
                de = time.strptime(endtime,fmt)

                startyear = starttime.split('-')[0]           
      
                tenure = diff(ds,de)
                offset = diff(ds,dc)	
	
                if int(startyear)==2012:
                   startyear = 2011
                if int(startyear)+tenure == 2012:
                   tenure -=1
                if int(startyear)+offset == 2012:
                   offset -=1

		statDict[str(startyear)+str(offset)+str(tenure)] += 1 #edits by start year, offset year, and tenure SSSSOT
		statDict["TOTAL"] += 1 #all edits done in namespace by user who started in start year (lines in file)
	
	for y in sorted(statDict.keys()):
		outputFileHandle.write(str(y) + ' ' + str(statDict[y]) + '\n') #prints offset year followed by # of edits
	
	inputFileHandle.close()
	outputFileHandle.close()

def diff (d1,d2):
       delta = d2[0] - d1[0]
       if d2[1] < d1[1]:
          delta -= 1
       elif  d2[1] == d1[1]:
          if d2[2] < d1[2]:
             delta -= 1
       return delta
      

main()
